﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Maticsoft.Common;
using QunYing.BLL;

namespace QunYing.Web.market
{
    public partial class market : System.Web.UI.Page
    {
        private string DealerID = "";
        protected override void OnInit(EventArgs e)
        {
            if (Session["userInfo"] == null)
            {
                Response.Redirect("~/Login.aspx");
            }
            else
            {
                DealerID = ((ArrayList)Session["userInfo"])[4].ToString();
            }
            base.OnLoad(e);

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                form1.DefaultButton = "btn_srh";
                lblUserName.Text = ((ArrayList)Session["userInfo"])[1].ToString();
                MenuBind(ParentRepeater, "0");
                ActivityBind();
                GoodsBind(HotRepeater, 4, "是否推荐", 0);
                GoodsBind(SaleRepeater, 4, "是否折价", 0);
                GoodsBind(ScoreRepeater, 4, "是否兑换", 0);
                OrderBind(5);
                NoticeBind();
            }
            lblNum.Text = Session["ShopCart"] != null ? ((DataTable)Session["ShopCart"]).Rows.Count.ToString() : "0";
        }

        /// <summary>
        /// 绑定活动
        /// </summary>
        private void ActivityBind()
        {
            DataTable dt = new BLL.ActivityGoods().GetActivityGoods(10, DealerID);
            ActivityRepeater.DataSource = dt;
            ActivityRepeater.DataBind();
            ActivityNumRepeater.DataSource = dt;
            ActivityNumRepeater.DataBind();
            if (dt.Rows.Count==1)
            {
//                lblOne.Text = @"<a href='" + dt.Rows[0]["类型"].ToString() + @"detail.aspx?id=" + dt.Rows[0]["商品ID"].ToString() + @"' style='width: 664px; height: 200px'>
//                <img src='" + dt.Rows[0]["宣传图片"].ToString() + "' width='664' height='200'></a>";
                lblOne.Text = @"<a href='#' style='width: 664px; height: 200px'><img src='" + dt.Rows[0]["宣传图片"].ToString() + "' width='664' height='200'></a>";
            }
            if (dt.Rows.Count ==0)
            {
                lblNo.Text = @"<img src='Pictures\Activity\no.jpg' width='664' height='200'>";
            }
        }

        protected void ParentRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater rp = (Repeater)e.Item.FindControl("ChildRepeater");
                string id = DataBinder.Eval(e.Item.DataItem, "类别id").ToString();
                MenuBind(rp, id);

            }
        }

        /// <summary>
        /// 绑定菜单
        /// </summary>
        private void MenuBind(Repeater rp, string parentId)
        {
            DataTable dt = new BLL.Category().GetChildCategory(parentId, DealerID);
            rp.DataSource = dt;
            rp.DataBind();
        }

        /// <summary>
        /// 绑定商品
        /// </summary>
        private void GoodsBind(Repeater rp, int top, string type, int catagory)
        {
            DataTable dt = new BLL.GoodsInfo().GetGoodsInfo(top, type, catagory, DealerID);
            rp.DataSource = dt;
            rp.DataBind();
        }
        /// <summary>
        /// 绑定排行
        /// </summary>
        private void OrderBind(int top)
        {
            DataTable dt = new BLL.GoodsInfo().GetGoodsOrder(top,DealerID);
            OrderRepeater.DataSource = dt;
            OrderRepeater.DataBind();
        }
        /// <summary>
        /// 绑定公告
        /// </summary>
        private void NoticeBind()
        {
            DataTable dt = new BLL.Notice().GetNotice();
            NoticeRepeater.DataSource = dt;
            NoticeRepeater.DataBind();
        }

        protected void btnExit_Click(object sender, EventArgs e)
        {
            Session.Remove("userInfo");
            Response.Redirect("~/Login.aspx");
        }

        protected void btn_srh_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Goods.aspx?query=" + Server.UrlEncode(inp_srh.Text.Trim()));
        }
    }
}
